import config from './config';
import store from '../store';

class apiRequest {
	constructor() {
		console.log('apiRequest');
	}
	request(options) {
		options.url = config + options.url;
		options.header = {
			'content-type': 'application/x-www-form-urlencoded'
		};
		options.method = 'POST';
		options.dataType = 'json';
		try {
			const token = store.state.token;
			if (token) {
				options.header = {
					'token': token,
					'content-type': 'application/x-www-form-urlencoded'
				};
			}
		} catch (err) {
			console.log(err)
		}

		return uni.request(options);
	}
	uploadFile(options) {
		options.url = config + options.url;
		options.dataType = 'json';
		try {
			const token = store.state.token;
			if (token) {
				options.header = {
					'token': token
				};
			}
		} catch (err) {
			console.log(err)
		}
		return uni.uploadFile(options);
	}
}

export default new apiRequest;
